package io.gbznt.modules.buss.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.gbznt.common.utils.PageUtils;
import io.gbznt.common.utils.R;
import io.gbznt.common.utils.ShiroUtils;
import io.gbznt.common.validator.ValidatorUtils;
import io.gbznt.common.validator.group.AddGroup;
import io.gbznt.modules.buss.entity.BussPublicNoticeEntity;
import io.gbznt.modules.buss.service.BussPublicNoticeService;

/**
 * 公告信息
 *
 * @author gbznt
 * @email http://www.gbznt.com/
 * @date 2019-04-20 15:48:33
 */
@RestController
@RequestMapping("buss/busspublicnotice")
public class BussPublicNoticeController {
	@Autowired
	private BussPublicNoticeService bussPublicNoticeService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("buss:busspublicnotice:list")
	public R list(@RequestParam Map<String, Object> params) {
		PageUtils page = bussPublicNoticeService.queryPage(params);

		return R.ok().put("page", page);
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("buss:busspublicnotice:info")
	public R info(@PathVariable("id") String id) {
		BussPublicNoticeEntity bussPublicNotice = bussPublicNoticeService.getById(id);

		return R.ok().put("bussPublicNotice", bussPublicNotice);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("buss:busspublicnotice:save")
	public R save(@RequestBody BussPublicNoticeEntity bussPublicNotice) {
		ValidatorUtils.validateEntity(bussPublicNotice, AddGroup.class);

		bussPublicNotice.setCreateId(ShiroUtils.getUserId() + "");
		bussPublicNotice.setCreateTime(new Date());
		bussPublicNotice.setCreateName(ShiroUtils.getUserName());
		bussPublicNoticeService.save(bussPublicNotice);

		return R.ok();
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("buss:busspublicnotice:update")
	public R update(@RequestBody BussPublicNoticeEntity bussPublicNotice) {
		ValidatorUtils.validateEntity(bussPublicNotice, AddGroup.class);

		bussPublicNotice.setUpdateId(ShiroUtils.getUserId() + "");
		bussPublicNotice.setUpdateTime(new Date());
		bussPublicNotice.setUpdateName(ShiroUtils.getUserName());
		Boolean re = bussPublicNoticeService.updateById(bussPublicNotice);// 全部更新

		return re ? R.ok() : R.oldVersion();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("buss:busspublicnotice:delete")
	public R delete(@RequestBody String[] ids) {
		bussPublicNoticeService.removeByIds(Arrays.asList(ids));

		return R.ok();
	}

	/**
	 * 改变公告信息状态
	 */
	@RequestMapping("/changeStatus")
	public R changeStatus(@RequestParam Map<String, Object> params) {
		String id = (String) params.get("id");
		BussPublicNoticeEntity bussPublicNotice = bussPublicNoticeService.getById(id);
		String status = (String) params.get("status");
		bussPublicNotice.setStatus(Integer.parseInt(status));
		Boolean re = bussPublicNoticeService.updateById(bussPublicNotice);// 全部更新
		return re ? R.ok() : R.oldVersion();
	}

}
